package com.ruoyi.yffy.controller;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.WordUtil;
import com.ruoyi.common.utils.uuid.UUID;
import com.ruoyi.yffy.domain.LldFj;
import com.ruoyi.yffy.domain.LldMx;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;

import java.io.InputStream;

import org.springframework.web.multipart.MultipartFile;
import com.ruoyi.yffy.domain.Lld;
import com.ruoyi.yffy.service.ILldService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 领料单Controller
 *
 * @author huacai
 * @date 2024-04-11
 */
@RestController
@RequestMapping("/yffy/lld")
public class LldController extends BaseController {
    @Autowired
    private ILldService lldService;

    /**
     * 查询领料单列表
     */
    @PreAuthorize("@ss.hasPermi('yffy:lld:list')")
    @GetMapping("/list")
    public TableDataInfo list(Lld lld) {
        startPage();
        List<Lld> list = lldService.selectLldList(lld);
        return getDataTable(list);
    }

    /**
     * 导出领料单列表
     */
    @PreAuthorize("@ss.hasPermi('yffy:lld:export')")
    @Log(title = "领料单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Lld lld) {
        List<Lld> list = lldService.selectLldList(lld);
        ExcelUtil<Lld> util = new ExcelUtil<Lld>(Lld.class);
        util.exportExcel(response, list, "领料单数据");
    }

    /**
     * 下载模板
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<Lld> util = new ExcelUtil<Lld>(Lld.class);
        util.importTemplateExcel(response, "领料单数据");
    }

    /**
     * 导入数据
     */
    @Log(title = "领料单", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('yffy:lld:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file) throws Exception {
        ExcelUtil<Lld> util = new ExcelUtil<Lld>(Lld.class);
        InputStream inputStream = file.getInputStream();
        List<Lld> list = util.importExcel(inputStream);
        inputStream.close();
        int count = lldService.batchInsertLld(list);
        return AjaxResult.success("导入成功" + count + "条信息！");
    }

    /**
     * 获取领料单详细信息
     */
    @PreAuthorize("@ss.hasPermi('yffy:lld:query')")
    @GetMapping(value = "/{lldId}")
    public AjaxResult getInfo(@PathVariable("lldId") Integer lldId) {
        return success(lldService.selectLldByLldId(lldId));
    }

    /**
     * 获取领料单明细详细信息
     */
    @PreAuthorize("@ss.hasPermi('yffy:lld:query')")
    @GetMapping(value = "/lldmx/{lldId}")
    public AjaxResult selectLldMxByLldId(@PathVariable("lldId") Integer lldId) {
        return success(lldService.selectLldMxByLldId(lldId));
    }

    /**
     * 根据主表ID查询所有金额的总和
     */
    @PreAuthorize("@ss.hasPermi('yffy:lld:query')")
    @GetMapping(value = "/lldmx/moneysum/{lldId}")
    public AjaxResult selectLldMxMoneySumByLldId(@PathVariable("lldId") Integer lldId) {
        return success(lldService.selectLldMxMoneySumByLldId(lldId));
    }


    /**
     * 新增领料单
     */
    @PreAuthorize("@ss.hasPermi('yffy:lld:add')")
    @Log(title = "领料单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Lld lld) {
        return toAjax(lldService.insertLld(lld));
    }

    /**
     * 修改领料单
     */
    @PreAuthorize("@ss.hasPermi('yffy:lld:edit')")
    @Log(title = "领料单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Lld lld) {
        return toAjax(lldService.updateLld(lld));
    }

    /**
     * 删除领料单
     */
    @PreAuthorize("@ss.hasPermi('yffy:lld:remove')")
    @Log(title = "领料单", businessType = BusinessType.DELETE)
    @DeleteMapping("/{lldIds}")
    public AjaxResult remove(@PathVariable Integer[] lldIds) {
        return toAjax(lldService.deleteLldByLldIds(lldIds));
    }


    /**
     * 新增附件
     */
    @PostMapping("/add/File/By/subId")
    public AjaxResult insertLlFjBylldId(@RequestBody LldFj lldFj) {
        return toAjax(lldService.insertLlFjBylldId(lldFj));
    }

    /**
     * 根据主表ID查询附件列表
     */
    @GetMapping("/get/FileList/By/subId")
    public AjaxResult selectLldFjList(Integer sId) {
        LldFj lldFj = new LldFj();
        lldFj.setLldId(sId);
        return AjaxResult.success(lldService.selectLldFjList(lldFj));
    }

    /**
     * 查询当前最新的主表主键ID
     */
    @ApiOperation("查询当前最新的主表主键ID")
    @GetMapping("/selectMAXId")
    public AjaxResult selectMAXId() {
        return AjaxResult.success(lldService.selectMAXId());
    }

    /**
     * 根据模板导出word
     *
     * @param response
     * @return
     */
    @Log(title = "根据模板导出word", businessType = BusinessType.EXPORT)
//    @PreAuthorize("@ss.hasPermi('occupation:basics:exportWord')")
    @GetMapping("/exportWord/{lldId}")
    public AjaxResult exportWord(@PathVariable("lldId") Integer lldId, HttpServletRequest request, HttpServletResponse response) {
        Lld lld = lldService.selectLldByLldId(lldId);
        //领料单位名称
        String LldwName = lld.getLldwName();
        //领用时间
        Date lyTimes = lld.getLyTime();
        // 创建一个 SimpleDateFormat 对象，指定日期格式
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        // 使用 SimpleDateFormat 对象将 Date 对象格式化为 String 对象
        String lyTime = sdf.format(lyTimes);
        //课题名称
        String ktName = lld.getKtName();
        //课题负责人
        String ktFzr = lld.getKtFzr();
        //有关事项说明
        String ygsxSm = lld.getYgsxSm();

        //领料单明细
        Lld lldMx = lldService.selectLldMxByLldId(lldId);
        List<LldMx> lldMxList = lldMx.getLldMxList();

        //金额总和
        Double moneySum = lldService.selectLldMxMoneySumByLldId(lldId);

        Map<String, Object> map = new HashMap<>();

        map.put("lldMxList", lldMxList); //领料单明细List
        map.put("LldwName", LldwName); //领料单位名称
        map.put("lyTime", lyTime); //领用时间
        map.put("ktName", ktName); //课题名称
        map.put("ktFzr", ktFzr); //课题负责人
        map.put("ygsxSm", ygsxSm); //有关事项说明
        map.put("moneySum", moneySum); //金额总和

        String str = "中国水电五局研发费用——领料单-" + ktName + ".docx";
        //获取yml配置地址
        String tempDir = "D:/ruoyi/uploadPath" + "/upload/";
//        String name = WordUtil.easyPoiExport("D:/ruoyi/111.docx", tempDir, str, map, request, response);
        String name = WordUtil.easyPoiExport("static/word/111.docx", tempDir, str, map, request, response);
        return AjaxResult.success(name);
    }

}
